package com.leo.foodmaster.admin.system.controller;

import com.leo.foodmaster.admin.system.entity.Permission;
import com.leo.foodmaster.admin.system.service.IPermissionService;
import com.leo.foodmaster.commons.annotation.Inner;
import com.leo.foodmaster.commons.entity.Result;
import com.leo.foodmaster.commons.entity.TreeSelect;
import com.leo.foodmaster.commons.enums.MenuTypeEnum;
import com.leo.foodmaster.commons.util.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author Leo
 * @since 2021-06-18
 */
@Api(tags = "用户权限管理")
@RestController
@AllArgsConstructor
@RequestMapping("/system/permission")
public class PermissionController {
    private final IPermissionService permissionService;

    @ApiOperation("查询菜单列表")
    @GetMapping("list")
    public Result<List<Permission>> listPermission(
            @ApiParam("菜单名称") @RequestParam(required = false) String title,
            @ApiParam("菜单状态") @RequestParam(required = false) String status) {
        List<Permission> list = permissionService.listPermission(title, status);
        return Result.success(list);
    }

    @ApiOperation("新增菜单")
    @PostMapping
    public Result<Boolean> addPermission(@RequestBody Permission permission) {
        if (MenuTypeEnum.CONTENT.getCode().equals(permission.getType())) {
            permission.setComponent("Layout");
        }
        if (MenuTypeEnum.CONTENT.getCode().equals(permission.getType())
                && permission.getParentId() != 0L) {
            permission.setComponent("ParentView");
        }
        if (!"F".equals(permission.getType())){
            permission.setName(permission.getPath().substring(0, 1).toUpperCase() + permission.getPath().substring(1));
        }
        boolean save = permissionService.save(permission);
        return Result.success(save);
    }

    @ApiOperation("修改菜单")
    @PutMapping
    public Result<Boolean> updatePermission(@RequestBody Permission permission) {
        if (MenuTypeEnum.CONTENT.getCode().equals(permission.getType())) {
            permission.setComponent("Layout");
        }
        boolean update = permissionService.updateById(permission);
        // 如果修改的类型为菜单，刷新Redis中的权限角色关系
        if (MenuTypeEnum.MENU.getCode().equals(permission.getType()) && update) {
            permissionService.refreshPermissionRolesToCache();
        }
        return Result.success(update);
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("/{id}")
    public Result<Object> deletePermission(@ApiParam("菜单Id") @PathVariable Long id) {
        return permissionService.deletePermission(id);
    }

    @Inner
    @ApiOperation("查询菜单树形列表")
    @GetMapping("treeSelect")
    public Result<List<TreeSelect>> treeSelect() {
        Long userId = WebUtils.getUser().getId();
        List<TreeSelect> list = permissionService.treeSelect(userId);
        return Result.success(list);
    }

    @Inner
    @ApiOperation("根据角色Id查询菜单Id列表")
    @GetMapping("IdsByRoleId/{id}")
    public Result<List<Long>> permissionIdsByRoleId(@ApiParam("角色Id") @PathVariable Long id) {
        List<Long> list = permissionService.permissionIdsByRoleId(id);
        return Result.success(list);
    }

}

